package org.turman.buobaobook.component;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

/**
 * 打印sql查询时间
 */
@Component
@Aspect
public class QueryPerformanceLoggingAspect {
    private Logger logger = LoggerFactory.getLogger(QueryPerformanceLoggingAspect.class);

    @Around("execution(* org.turman.buobaobook.mapper..*(..))")
    public Object logQueryPerformance(ProceedingJoinPoint joinPoint) throws Throwable
    {
        long startTime = System.currentTimeMillis();
        try {
            Object result = joinPoint.proceed(); // 继续执行被拦截的方法
            return result;
        } finally {
            long elapsedTime = System.currentTimeMillis() - startTime;
            String methodName = joinPoint.getSignature().getName();
            logger.info("SQL time ------> Method " + methodName + " executed in " + elapsedTime + "ms");
        }
    }
}
